package com.wxp.alice.lanfilemaster.Server.Base;

import java.nio.charset.StandardCharsets;

import fi.iki.elonen.NanoHTTPD;
import fi.iki.elonen.NanoHTTPD.IHTTPSession;
import fi.iki.elonen.NanoHTTPD.Method;
import fi.iki.elonen.NanoHTTPD.Response.Status;

public abstract class BaseRequestHandler {
    private IHTTPSession mSession;
    private Status mStatus = Status.OK;
    private String mMimeType = NanoHTTPD.MIME_PLAINTEXT;
    private byte[] mData;

    public BaseRequestHandler() {

    }

    public void setSession(IHTTPSession session) {
        mSession = session;
    }

    public void setStatus(Status status) {
        mStatus = status;
    }

    public void setMimeType(String mimeType) {
        mMimeType = mimeType;
    }

    public Status getStatus() {
        return mStatus;
    }

    public String getMimeType() {
        return mMimeType;
    }

    protected void write(String text) {
        mData = text.getBytes(StandardCharsets.UTF_8);
    }

    protected void write(byte[] bytes) {
        mData = bytes;
    }

    public byte[] getResponseData() {
        if (mData == null) {
            mData = new byte[]{0x00};
        }
        return mData;
    }

    public void handleMethod(Method method) {
        switch (method) {
            case POST:
                post();
                break;
            case GET:
                get();
                break;
            case HEAD:
                head();
                break;
            case PUT:
                put();
                break;
            case DELETE:
                delete();
                break;
            default:
                undefined();
                break;
        }
    }

    protected void post() {
    }

    protected void get() {
    }

    protected void head() {
    }

    protected void put() {
    }

    protected void delete() {
    }

    protected void undefined() {
    }
}
